package away3d.animators.skeleton;

/**
 * Author: David Lenaerts
 */


import flash.geom.Vector3D ;



class SkeletonBinaryLERPNode extends SkeletonTreeNode
{
    
    
    public var inputA : SkeletonTreeNode;
    public var inputB : SkeletonTreeNode;
    private var _blendWeight : Number;
    
    
    
    public function new()
    {
        
        super() ;
        
    }
    
    override public var time( null, set_time ): Float ;
    
    override private function set_time( value: Float ) : Float
    {
        
        inputA.time         = value;
        inputB.time         = value;
        super.time          = value;
        return value ;
        
    }
    
    
    override public var time( null, set_time ): Float ;
    
    
    override private function set_direction( value : Float ) : Float
    {
        
        inputA.direction    = value;
        inputB.direction    = value;
        super.direction     = value;
        
        return value ;
        
    }
    
    
    override public var blendWeight( get_blendWeight, set_blendweight ) : Float ;
    
    
    public function get_blendWeight() : Float 
    {
        
        return _blendWeight;
        
    }
    
    
    public function set_blendWeight( value : Float ) : Float
    {
        
        _blendWeight = value;
        _duration    = inputA.duration + _blendWeight*( inputB.duration - inputB.duration );
        
        return value ;
        
    }
    
    
// todo: return whether or not update was performed
    override public function updatePose( skeleton : Skeleton )
    {
        
        // todo: should only update if blendWeight dirty, or if either child returns false
        inputA.updatePose( skeleton ) ;
        inputB.updatePose( skeleton ) ;
        
        var endPose     : JointPose ;
        var endPoses                        = skeletonPose.jointPoses ;
        var poses1                          = inputA.skeletonPose.jointPoses ;
        var poses2                          = inputB.skeletonPose.jointPoses ;
        var pose1       : JointPose ; 
        var pose2       : JointPose ;
        var p1          : Vector3D<Float> ; 
        var p2          : Vector3D<Float> ;
        var tr          : Vector3D<Float> ;
        var numJoints                       = skeleton.numJoints ;
        
        // :s
        if( endPoses.length != numJoints ) endPoses.length = numJoints ;
        
        for( i in 0...numJoints )
        {
            
            endPose = endPoses[ i ];
            if( endPose == null ) endPose = new JointPose();
            pose1   = poses1[ i ] ;
            pose2   = poses2[ i ] ;
            p1      = pose1.translation ; 
            p2      = pose2.translation ;
            
            endPose.orientation.lerp( pose1.orientation, pose2.orientation, blendWeight ) ;
            
            tr      = endPose.translation ;
            tr.x    = p1.x + blendWeight*( p2.x - p1.x ) ;
            tr.y    = p1.y + blendWeight*( p2.y - p1.y ) ;
            tr.z    = p1.z + blendWeight*( p2.z - p1.z ) ;
            
        }
        
    }
    
    
    override public function updatePositionData()
    {
        
        var deltA   = inputA.rootDelta ;
        var deltB   = inputB.rootDelta ;
        rootDelta.x = deltA.x + blendWeight*( deltB.x - deltA.x ) ;
        rootDelta.y = deltA.y + blendWeight*( deltB.y - deltA.y ) ;
        rootDelta.z = deltA.z + blendWeight*( deltB.z - deltA.z ) ;
        
    }
    
    
}
